package com.dev.dream.exochatlockscreen;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dev.dream.exochatlockscreen.service.SLockscreen;

public class SetPasscodeActivity extends Activity {

	TextView btnDelete, btnCancel;
	// Shimmer shimmer;

	ImageView btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0,
			btn_pin_1, btn_pin_2, btn_pin_3, btn_pin_4, imgPasswordFail;

	TextView tvInfoPasscode;
	boolean isInputPasscode = true;
	Animation aSetPassword;
	RelativeLayout layoutPin;
	ImageView imgBgPasscode;

	@Override
	protected void attachBaseContext(Context newBase) {
		super.attachBaseContext(new CalligraphyContextWrapper(newBase));
	}

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		CalligraphyConfig.initDefault("fonts/ttt.ttf", R.attr.fontPath);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_set_passcode);

		if (SLockscreen.instance == null) {

			startService(new Intent(getApplicationContext(), SLockscreen.class));

		}

		if (EXOChatKeypadPreferences.getInstance().getValuePasscode()
				.equals("")) {
			Toast.makeText(getApplicationContext(), "Start set your passcode!",
					1000).show();
		} else {
			Toast.makeText(getApplicationContext(),
					"Start change your passcode!", 1000).show();
		}

		layoutPin = (RelativeLayout) findViewById(R.id.rPin);
		imgBgPasscode = (ImageView) findViewById(R.id.imgBgPasscode);
		btnDelete = (TextView) findViewById(R.id.btnDelete);
		btnCancel = (TextView) findViewById(R.id.btnCancel);
		btn_pin_1 = (ImageView) findViewById(R.id.btn_pin_1);
		btn_pin_2 = (ImageView) findViewById(R.id.btn_pin_2);
		btn_pin_3 = (ImageView) findViewById(R.id.btn_pin_3);
		btn_pin_4 = (ImageView) findViewById(R.id.btn_pin_4);
		tvInfoPasscode = (TextView) findViewById(R.id.tv_info_passcode);

		imgPasswordFail = (ImageView) findViewById(R.id.imgPasswordFail);
		imgPasswordFail.setVisibility(View.GONE);

		// shimmer = new Shimmer();
		// shimmer.start(btnDelete);
		// shimmer.start(btnCancel);

		// try {
		// layoutPin
		// .setBackgroundDrawable(SLockscreen.instance.backgroundBlur[EXOChatKeypadPreferences
		// .getInstance().getImgIndexBg()]);
		// } catch (Exception ex) {
		// }

		try {
			imgBgPasscode
					.setImageBitmap(SLockscreen.instance.background[EXOChatKeypadPreferences
							.getInstance().getImgIndexBg()]);
		} catch (Exception ex) {
		}

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (EXOChatKeypadPreferences.getInstance().getValuePasscode()
						.equals("")) {
					EXOChatKeypadPreferences.getInstance().setEnablePasscode(
							false);
					Toast.makeText(getApplicationContext(),
							"Cancel Enter your Passcode", 1000).show();
				} else {
					Toast.makeText(getApplicationContext(),
							"Cancel Change your Passcode", 1000).show();
				}

				finish();
			}
		});

		btnDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					deleteConfirm();
				} else {
					deletePin();
				}
			}
		});

		aSetPassword = AnimationUtils.loadAnimation(this, R.anim.fail_password);

		btn1 = (ImageView) findViewById(R.id.btn1);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(1);
				} else {
					setPin(1);
				}
			}
		});

		btn2 = (ImageView) findViewById(R.id.btn2);
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(2);
				} else {
					setPin(2);
				}
			}
		});

		btn3 = (ImageView) findViewById(R.id.btn3);
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(3);
				} else {
					setPin(3);
				}
			}
		});

		btn4 = (ImageView) findViewById(R.id.btn4);
		btn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(4);
				} else {
					setPin(4);
				}
			}
		});

		btn5 = (ImageView) findViewById(R.id.btn5);
		btn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(5);
				} else {
					setPin(5);
				}
			}
		});

		btn6 = (ImageView) findViewById(R.id.btn6);
		btn6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(6);
				} else {
					setPin(6);
				}
			}
		});

		btn7 = (ImageView) findViewById(R.id.btn7);
		btn7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(7);
				} else {
					setPin(7);
				}
			}
		});

		btn8 = (ImageView) findViewById(R.id.btn8);
		btn8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(8);
				} else {
					setPin(8);
				}
			}
		});

		btn9 = (ImageView) findViewById(R.id.btn9);
		btn9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(9);
				} else {
					setPin(9);
				}
				;
			}
		});

		btn0 = (ImageView) findViewById(R.id.btn0);
		btn0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isInputPasscode) {
					setConfirmPin(0);
				} else {
					setPin(0);
				}
			}
		});
	}

	int index_pin = 1;
	String input_passcode = "";

	public void setPin(int value) {

		if (index_pin < 5) {
			input_passcode += value;

			switch (index_pin) {
			case 1:
				btn_pin_1.setSelected(true);
				break;

			case 2:
				btn_pin_2.setSelected(true);
				break;
			case 3:
				btn_pin_3.setSelected(true);
				break;
			case 4:
				btn_pin_4.setSelected(true);

				if (isInputPasscode) {
					isInputPasscode = false;

					tvInfoPasscode.setText("Confirm your Passcode");
					index_pin = 0;
					btn_pin_1.setSelected(false);
					btn_pin_2.setSelected(false);
					btn_pin_3.setSelected(false);
					btn_pin_4.setSelected(false);

				}

				break;
			}
			index_pin++;
		}
	}

	String confirm_passcode = "";

	@SuppressLint("NewApi")
	public void setConfirmPin(int value) {

		if (index_pin < 5) {
			confirm_passcode += value;

			switch (index_pin) {
			case 1:
				btn_pin_1.setSelected(true);
				break;

			case 2:
				btn_pin_2.setSelected(true);
				break;
			case 3:
				btn_pin_3.setSelected(true);
				break;
			case 4:
				btn_pin_4.setSelected(true);

				if (confirm_passcode.equals(input_passcode)) {
					if (EXOChatKeypadPreferences.getInstance()
							.getValuePasscode().equals("")) {
						Toast.makeText(getApplicationContext(),
								"Set your Passcode success!",
								Toast.LENGTH_SHORT);
					} else {
						Toast.makeText(getApplicationContext(),
								"Change your Passcode success!",
								Toast.LENGTH_SHORT).show();
					}

					EXOChatKeypadPreferences.getInstance().setValuePasscode(
							input_passcode);
					finish();
				} else {

					Toast.makeText(getApplicationContext(),
							"Confirm your Passcode not match!!!",
							Toast.LENGTH_SHORT).show();

					imgPasswordFail.setVisibility(View.VISIBLE);

					imgPasswordFail.startAnimation(aSetPassword);
					aSetPassword.setAnimationListener(new AnimationListener() {

						@Override
						public void onAnimationStart(Animation animation) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationRepeat(Animation animation) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationEnd(Animation animation) {
							// TODO Auto-generated method stub
							isInputPasscode = true;
							input_passcode = "";
							confirm_passcode = "";
							tvInfoPasscode.setText("Enter your Passcode");
							index_pin = 1;
							btn_pin_1.setSelected(false);
							btn_pin_2.setSelected(false);
							btn_pin_3.setSelected(false);
							btn_pin_4.setSelected(false);
							imgPasswordFail.setVisibility(View.GONE);
						}
					});

				}

				break;
			}
			index_pin++;
		}
	}

	String tempInput;

	public void deletePin() {

		switch (index_pin) {
		case 2:

			btn_pin_1.setSelected(false);
			input_passcode = "";
			index_pin = 1;
			break;

		case 3:
			btn_pin_2.setSelected(false);
			index_pin = 2;
			tempInput = "";
			tempInput += input_passcode.charAt(0);
			input_passcode = tempInput;
			break;
		case 4:
			btn_pin_3.setSelected(false);
			index_pin = 3;
			tempInput = "";
			tempInput += input_passcode.charAt(0);
			tempInput += input_passcode.charAt(1);
			input_passcode = tempInput;
			break;
		}
	}

	public void deleteConfirm() {

		switch (index_pin) {
		case 2:

			btn_pin_1.setSelected(false);
			confirm_passcode = "";
			index_pin = 1;
			break;

		case 3:
			btn_pin_2.setSelected(false);
			index_pin = 2;
			tempInput = "";
			tempInput += confirm_passcode.charAt(0);
			confirm_passcode = tempInput;
			break;
		case 4:
			btn_pin_3.setSelected(false);
			index_pin = 3;
			tempInput = "";
			tempInput += confirm_passcode.charAt(0);
			tempInput += confirm_passcode.charAt(1);
			confirm_passcode = tempInput;
			break;
		}
	}
}
